import { defineStore } from 'pinia';
import myAxios from '../axios/myAxios';
import { ElMessage } from 'element-plus'
export const userAuthStore = defineStore('userAuth', {
    state: () => ({
        token: localStorage.getItem('token') || '',
        user: JSON.parse(localStorage.getItem('user') || 'null')
    }),
    //方法
    actions: {
        //登录
        async login(params) {
            // 调用登录 API
            await myAxios.post('/lhhUser/login', params)
                .then((repeson) => {
                    var repesonData = repeson.data.data;
                    // 更新状态和本地存储
                    this.user = repesonData.user;
                    this.token = repesonData.token.toke;
                    // 持久化存储
                    localStorage.setItem('token', repesonData.token.toke);
                    localStorage.setItem('user', JSON.stringify(repesonData.user));
                    ElMessage({ message: '登录成功', type: 'success' });
                })
                .catch((error) => {
                    ElMessage({ message: '登录失败', type: error });
                    throw error;
                })
        }
    },
});